package com.example.main.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.example.common.utils.PageUtils;
import com.example.common.utils.R;
import com.example.main.controller.vo.ServiceVO;
import com.example.main.controller.vo.UserVO;
import com.example.main.entity.ServicesEntity;
import com.example.main.service.HistoryService;
import com.example.main.service.ServicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.sql.Wrapper;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("")
public class MainController {
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    ServicesService servicesService;

    @Autowired
    HistoryService historyService;


    //服务列表
    @GetMapping("/servicesList")
    public R getServices() {
        List<ServicesEntity> servicesNames = servicesService.list();
        List<ServiceInstance> services = new ArrayList<>();

        for (ServicesEntity service : servicesNames) {
            List<ServiceInstance> sis = discoveryClient.getInstances(service.getServiceName());

            if(sis.size() == 0) {
                DefaultServiceInstance offLine = new DefaultServiceInstance(service.getServiceName(), "", 0, false);
                services.add(offLine);
            }
            services.addAll(sis);
        }

        //排序
        List<ServiceInstance> sorted = services.stream().sorted((s1, s2) -> {
            return s1.getPort() != 0 ? -1 : 1;
        }).collect(Collectors.toList());
        return R.ok().put("data", sorted);
    }

    //服务实例管理（所有服务实例）--数据库获取
    @PostMapping("/allServices")
    public R getServices(@RequestBody ServiceVO serviceVO) {
        System.out.println(JSON.toJSONString(serviceVO));
        PageUtils page = servicesService.queryPage(serviceVO);
        return R.ok().put("page", page);
    }

    //所有服务实例--nacos获取
    @GetMapping(value = "/allInstances")
    public R get() {
        Map<String, List<ServiceInstance>> msl = new HashMap<>();
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            List<ServiceInstance> sis = discoveryClient.getInstances(service);
            msl.put(service, sis);
        }
        return R.ok()
                .put("instances", msl);
    }

//    //查找服务
//    @PostMapping("/searchService")
//    public R searchService(@RequestBody ServiceVO serviceVO) {
//        servicesService.queryPage(serviceVO);
//        return R.ok().put("data", "ok add").put("size", 18);
//    }

    //添加服务
    @PostMapping("/addService")
    public R addService(@RequestBody ServicesEntity servicesEntity) {
        servicesService.save(servicesEntity);
        return R.ok().put("data", "ok add").put("size", 18);
    }

    //删除服务
    @PostMapping("/delService")
    public R delService(@RequestBody ServicesEntity servicesEntity) {
        servicesService.removeById(servicesEntity.getId());
        return R.ok().put("data", "ok delete").put("size", 18);
    }

    //修改服务
    @PostMapping("/updService")
    public R updService(@RequestBody ServicesEntity servicesEntity) {
        servicesService.saveOrUpdate(servicesEntity);
        return R.ok().put("data", "ok update").put("size", 18);
    }

    //历史服务运行记录
    @GetMapping("/historyList")
    public R getHistories(@RequestParam Map<String, Object> params) {
        PageUtils page = historyService.queryPage(params);
        return R.ok().put("page", page);
    }
}
